//首次定位
$$.ajax({
	    url:"http://192.168.5.22:8080/webmeeting/user/location",
	    method:"GET",
		data:{},
		dataType:'json',
		timeout:getLoctimeout,
		success: function (ajaxresult) {
			ajaxresult = ajaxresult.split(':');
			//楼层
			var floor = parseInt(ajaxresult[0]);
			var cblocs;
			switch(floor){
				case 1:
					cblocs = blocks1;
					break;
				case 3:
					cblocs = blocks3;
					break;
				default:
					cblocs = '';
			}
			//实际坐标
			var cX = parseFloat(ajaxresult[1]);
			var cY = parseFloat(ajaxresult[2]);
			//网格坐标
			var cbx = Math.abs(Math.round(cX/mapRealW*netNum.x)-1);
			var cby = Math.abs(Math.round(cY/mapRealH*netNum.y)-1);
			if(getLocDebug){
				var blind;
				if(cblocs[cby][cbx]==0){
					blind = 'No';
				}else{
					blind = 'Yes';
				}
				myApp.alert('接口返回：floor='+floor+';x='+cX+';y='+cY+'<br>转换后的网格坐标：['+cbx+','+cby+']<br>'+'该点是否屏蔽：'+blind);
			}
			//目前的位置
			var cobx = parseInt($$('.current-icon').data('blockX'));
			var coby = parseInt($$('.current-icon').data('blockY'));
			var cof = parseInt($$('.current-icon').attr('floor'));
			var det = parseInt($$('.current-icon').data('detectFloor'));
			if(cblocs[cby][cbx]==0){
				//获取的位置与目前的位置不同
				if(cbx != cobx || cby != coby || floor != cof ){
					currentBlockX = cbx;
					currentBlockY = cby;
					$$('.current-icon').data('blockX',currentBlockX);
					$$('.current-icon').data('blockY',currentBlockY);
					$$('.current-icon').attr('floor',floor);
					setCurrentPos();
					//首次定位，切换楼层
					if(det == 1){
						$$('.current-icon').data('detectFloor',0);
						switchFloor(floor.toString());
					}
				}
			}
		},
		error:function(){
			myApp.alert('请求失败');
		}
});
//每隔五秒刷新定位
var getLoctimer =  setInterval(function(){
	$$.ajax({
		    url:"http://192.168.5.22:8080/webmeeting/user/location",
		    method:"GET",
			data:{},
			dataType:'json',
			timeout:getLoctimeout,
			success: function (ajaxresult) {
				ajaxresult = ajaxresult.split(':');
				//楼层
				var floor = parseInt(ajaxresult[0]);
				var cblocs;
				switch(floor){
					case 1:
						cblocs = blocks1;
						break;
					case 3:
						cblocs = blocks3;
						break;
					default:
						cblocs = '';
				}
				//实际坐标
				var cX = parseFloat(ajaxresult[1]);
				var cY = parseFloat(ajaxresult[2]);
				//网格坐标
				var cbx = Math.abs(Math.round(cX/mapRealW*netNum.x)-1);
				var cby = Math.abs(Math.round(cY/mapRealH*netNum.y)-1);
				if(getLocDebug){
					var blind;
					if(cblocs[cby][cbx]==0){
						blind = 'No';
					}else{
						blind = 'Yes';
					}
					myApp.alert('接口返回：floor='+floor+';x='+cX+';y='+cY+'<br>转换后的网格坐标：['+cbx+','+cby+']<br>'+'该点是否屏蔽：'+blind);
				}
				//目前的位置
				var cobx = parseInt($$('.current-icon').data('blockX'));
				var coby = parseInt($$('.current-icon').data('blockY'));
				var cof = parseInt($$('.current-icon').attr('floor'));
				var det = parseInt($$('.current-icon').data('detectFloor'));
				if(cblocs[cby][cbx]==0){
					//获取的位置与目前的位置不同
					if(cbx != cobx || cby != coby || floor != cof ){
						currentBlockX = cbx;
						currentBlockY = cby;
						$$('.current-icon').data('blockX',currentBlockX);
						$$('.current-icon').data('blockY',currentBlockY);
						$$('.current-icon').attr('floor',floor);
						setCurrentPos();
						//首次定位，切换楼层
						if(det == 1){
							$$('.current-icon').data('detectFloor',0);
							switchFloor(floor.toString());
						}
					}
				}
			},
			error:function(){
				myApp.alert('请求失败');
			}
	});
},getLoctime);

/*测试*/
/*var testRoute = [
                 [19,17],[19,19],[19,21],[19,23],[19,25],[19,28],[17,29],[16,28],[14,28],[12,16],
                 [11,26],[13,27],[13,29],[13,31],[15,31],[17,31],[17,33],[17,35],[17,38],[19,38],
                 [21,38],[24,38],[25,41],[25,43],[27,46],[28,47],[29,49]
];
var cf='01';
var ri = 0;
var testtimer = setInterval(function(){
	if(ri>9){
		cf='03';
	}
	if(ri>26){
		clearInterval(testtimer);
		return;
	}
	var cbx = testRoute[ri][0];
	var cby= testRoute[ri][1];
	cf = parseInt(cf);
	var cblocs;
	switch(cf){
		case 1:
			cblocs = blocks1;
			break;
		case 3:
			cblocs = blocks3;
			break;
		default:
			cblocs = '';
	}
	if(getLocDebug){
		var blind;
		if(cblocs[cby][cbx]==0){
			blind = 'No';
		}else{
			blind = 'Yes';
		}
		myApp.alert('接口返回：floor='+cf+'<br>转换后的网格坐标：['+cbx+','+cby+']<br>'+'该点是否屏蔽：'+blind);
	}
	//目前的位置
	var cobx = parseInt($$('.current-icon').data('blockX'));
	var coby = parseInt($$('.current-icon').data('blockY'));
	var cof = parseInt($$('.current-icon').attr('floor'));
	var det = parseInt($$('.current-icon').data('detectFloor'));
	//console.log(cbx,cby,cblocs[cby][cbx]);
	if(cblocs[cby][cbx]==0){
		//获取的位置与目前的位置不同
		if(cobx != cbx || coby != cby ||cof != cf){
			currentBlockX = cbx;
			currentBlockY = cby;
			$$('.current-icon').data('blockX',currentBlockX);
			$$('.current-icon').data('blockY',currentBlockY);
			$$('.current-icon').attr('floor',cf);
			setCurrentPos();
			//首次定位，切换楼层
			if(det == 1){
				$$('.current-icon').data('detectFloor',0);
				switchFloor(cf.toString());
			}
		}
	}
	ri += 1;
},2000);*/


